//
//  HJAlertView.m
//  UI
//
//  Created by will on 16/3/23.
//  Copyright © 2016年 shangtv. All rights reserved.
//

#import "HJAlertView.h"


#define HAlertWidth 245.0f
#define HAlertHeight 160.0f
@interface HJAlertView ()
{
   BOOL  _leftLeave;//判断提示框的旋转方向
}

@property (nonatomic, strong) UILabel *alertTitleLabel;
@property (nonatomic, strong) UILabel *alertContentLabel;
@property (nonatomic, strong) UIButton *leftBtn;
@property (nonatomic, strong) UIButton *rightBtn;

@property (nonatomic, strong) UIView *backImageView;


@end

#define HTitleYOffset 15.0f
#define HTitleHeight 25.0f

#define HContentOffset 30.0f
#define HBetweenLabelOffset 20.0f

#define HSingleButtonWidth 160.0f
#define HCoupleButtonWidth 107.0f
#define HButtonHeight 40.0f
#define HButtonBottomOffset 10.0f

@implementation HJAlertView


-(instancetype)initWithTitle:(NSString *)title
                 contentText:(NSString *)content
             leftButtonTitle:(NSString *)leftTitle
            rightButtonTitle:(NSString *)rigthTitle{
    
    if (self = [super init]) {

        self.layer.cornerRadius = 5.0;
        self.backgroundColor = [UIColor whiteColor];
        
        self.alertTitleLabel.text = title;
        self.alertContentLabel.text = content;

        if (!leftTitle) {
            
            self.rightBtn.frame = [self rightBtnFrame];
            [self.rightBtn setTitle:rigthTitle forState:UIControlStateNormal];
            [self.rightBtn addTarget:self action:@selector(rightBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
        }else{
            
            [self.rightBtn setTitle:rigthTitle forState:UIControlStateNormal];
            [self.rightBtn addTarget:self action:@selector(rightBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
            
            [self.leftBtn setTitle:leftTitle forState:UIControlStateNormal];
            [self.leftBtn addTarget:self action:@selector(leftBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
        }
        
        
        UIButton *xButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [xButton setImage:[UIImage imageNamed:@"btn_close_normal.png"] forState:UIControlStateNormal];
        [xButton setImage:[UIImage imageNamed:@"btn_close_selected.png"] forState:UIControlStateHighlighted];
        xButton.frame = CGRectMake(HAlertWidth - 32, 0, 32, 32);
        [self addSubview:xButton];
        [xButton addTarget:self action:@selector(dismissAlert) forControlEvents:UIControlEventTouchUpInside];
        
        self.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin;
        
    }
    return self;
}

-(void)layoutSubviews{
    [super layoutSubviews];
    
}

- (void)leftBtnClicked:(id)sender{
    
    _leftLeave = YES;
    
    [self dismissAlert];
    if (self.leftBtnBlock) {
        self.leftBtnBlock();
    }
    
}

- (void)rightBtnClicked:(id)sender{
    
    _leftLeave = NO;
    
    [self dismissAlert];
    if ((self.rightBtnBlock)) {
        self.rightBtnBlock();
    }
    
}

- (void)dismissAlert{
    
    [self removeFromSuperview];
    if (self.dissmiss) {
        self.dissmiss();
    }
    
}

-(void)show{
    
    UIViewController *topVC = [self appRootViewController];
    self.frame = CGRectMake((CGRectGetWidth(topVC.view.bounds) - HAlertWidth) * 0.5, - HAlertHeight - 30, HAlertWidth, HAlertHeight);
    [topVC.view addSubview:self];
    
}

- (UIViewController *)appRootViewController
{
    UIViewController *appRootVC = [UIApplication sharedApplication].keyWindow.rootViewController;
    UIViewController *topVC = appRootVC;
    while (topVC.presentedViewController) {
        topVC = topVC.presentedViewController;
    }
    return topVC;
}

- (void)removeFromSuperview{
    
    [self.backImageView removeFromSuperview];
    self.backImageView = nil;
    
    UIViewController *topVC = [self appRootViewController];
    CGRect afterFrame = CGRectMake((CGRectGetWidth(topVC.view.bounds) - HAlertWidth) * 0.5, CGRectGetHeight(topVC.view.bounds), HAlertWidth, HAlertHeight);
    
    [UIView animateWithDuration:0.35f delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^{
        
        self.frame = afterFrame;
        
        if (_leftLeave) {
            self.transform = CGAffineTransformMakeRotation(-M_1_PI / 1.5);
            
        }else {
            
            self.transform = CGAffineTransformMakeRotation(M_1_PI / 1.5);
        }
    } completion:^(BOOL finished) {
        
        [super removeFromSuperview];
    }];
}

- (void)willMoveToSuperview:(UIView *)newSuperview
{
    if (newSuperview == nil) {
        return;
    }
    
    UIViewController *topVC = [self appRootViewController];
    
    if (!self.backImageView) {
        self.backImageView = [[UIView alloc] initWithFrame:topVC.view.bounds];
        self.backImageView.backgroundColor = [UIColor blackColor];
        self.backImageView.alpha = 0.6f;
        self.backImageView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    }
    
    [topVC.view addSubview:self.backImageView];
    
    self.transform = CGAffineTransformMakeRotation(-M_1_PI / 2);
    
    CGRect afterFrame = CGRectMake((CGRectGetWidth(topVC.view.bounds) - HAlertWidth) * 0.5, (CGRectGetHeight(topVC.view.bounds) - HAlertHeight) * 0.5, HAlertWidth, HAlertHeight);
    
    [UIView animateWithDuration:0.35f delay:0.0 options:UIViewAnimationOptionCurveEaseIn animations:^{
        
        self.transform = CGAffineTransformMakeRotation(0);
        self.frame = afterFrame;
    } completion:^(BOOL finished) {
    }];
    [super willMoveToSuperview:newSuperview];
}



//当只有一个Btn
- (CGRect)rightBtnFrame{
    

    CGFloat leftBtnX = (HAlertWidth - HSingleButtonWidth) * 0.5;
    CGFloat leftBtnY = HAlertHeight - HButtonBottomOffset - HButtonHeight;
    CGFloat leftBtnWidth  = HSingleButtonWidth;
    CGFloat leftBtnHeight = HButtonHeight;
    
    return CGRectMake(leftBtnX, leftBtnY, leftBtnWidth, leftBtnHeight);
}

#pragma mark - 懒加载

- (UILabel*)alertTitleLabel{
    
    if (_alertTitleLabel == nil) {
        
        _alertTitleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, HTitleYOffset, HAlertWidth, HTitleHeight)];
 
        _alertTitleLabel.font   = [UIFont systemFontOfSize:20.0];
        _alertTitleLabel.textColor = [UIColor colorWithRed:56/255.0 green:64/255.0 blue:71/255.0 alpha:1.0];
        _alertTitleLabel.textAlignment = NSTextAlignmentCenter;
        
        [self addSubview:_alertTitleLabel];

    }
    return _alertTitleLabel;
}

- (UILabel*)alertContentLabel{
    
    if (_alertContentLabel == nil) {

        _alertContentLabel = [[UILabel alloc]initWithFrame:[self aletrContentLabelFrame]];
        _alertContentLabel.numberOfLines = 0;
        _alertContentLabel.textAlignment = NSTextAlignmentCenter;
        _alertContentLabel.font   = [UIFont systemFontOfSize:15.0];
        _alertContentLabel.textColor = [UIColor colorWithRed:127/255.0 green:127/255.0 blue:127/255.0 alpha:1.0];
        
        [self addSubview:_alertContentLabel];
    }
    return _alertContentLabel;
}

- (CGRect)aletrContentLabelFrame{
    CGFloat contentLabelWidth  = HAlertWidth;
    CGFloat contentLabelHeight = 60;
    CGFloat contentLabelX = 0;
    CGFloat contentLabelY = CGRectGetMaxY(self.alertTitleLabel.frame);
    
    return CGRectMake(contentLabelX, contentLabelY, contentLabelWidth, contentLabelHeight);
}

- (UIButton*)leftBtn{
    if (_leftBtn == nil) {
        
        _leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _leftBtn.titleLabel.font = [UIFont systemFontOfSize:14.0];
        _leftBtn.frame = [self btnFrame:YES];
        
        [_leftBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_leftBtn setBackgroundImage:[UIImage imageWithColor:[UIColor colorWithRed:227/255.0 green:100/255.0 blue:83/255.0 alpha:1]] forState:UIControlStateNormal];
        
        [self addSubview:_leftBtn];
    }
    return _leftBtn;
}


- (UIButton*)rightBtn{
    if (_rightBtn == nil) {
        
        _rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _rightBtn.titleLabel.font = [UIFont systemFontOfSize:14.0];
        _rightBtn.frame = [self btnFrame:NO];
        
        
        [_rightBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_rightBtn setBackgroundImage:[UIImage imageWithColor:[UIColor colorWithRed:87/255.0 green:135/255.0 blue:173/255.0 alpha:1]] forState:UIControlStateNormal];
        
        [self addSubview:_rightBtn];
    }
    return _rightBtn;
}

- (CGRect)btnFrame:(BOOL)isLeftBtn{
    CGFloat BtnX = (HAlertWidth - 2 * HCoupleButtonWidth - HButtonBottomOffset) * 0.5;
    CGFloat BtnY = HAlertHeight - HButtonBottomOffset -HButtonHeight;
    CGFloat BtnWidth  = HCoupleButtonWidth;
    CGFloat BtnHeight = HButtonHeight;
    
    CGRect frame = CGRectMake(BtnX, BtnY, BtnWidth, BtnHeight);
    if (!isLeftBtn) {
        BtnX = (CGRectGetMaxX(frame) + HButtonBottomOffset);
        frame.origin.x = BtnX;
    }
    return frame;
    
}

@end



@implementation UIImage (colorful)

+ (UIImage *)imageWithColor:(UIColor *)color
{
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return image;
}

@end
